using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace teavch.Utilities.Electrical.Design.Gui.LineDiagram
{
	public partial class Transformer : ElectricalSymbol
	{

		private float _trRatio = .8f;
		private float _topHandRatio = .5f;
		private float _bottomHandRatio = .5f;
		private TrasformerType _type = TrasformerType.CurrentTrasformer;

		public Transformer()
		{
			InitializeComponent();
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the type of the Transformer" )]
		public TrasformerType Type
		{
			get { return this._type; }
			set
			{
				this._type = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the Top Link Line ration of the top node of the Transformer" )]
		public float TopHandRatio
		{
			get { return this._topHandRatio; }
			set
			{
				this._topHandRatio = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the Bottom Link Line ration of the bottom node of the Transformer" )]
		public float BottomHandRatio
		{
			get { return this._bottomHandRatio; }
			set
			{
				this._bottomHandRatio = value;
				this.Refresh();
			}
		}

		protected override void OnPaint( PaintEventArgs e )
		{
			base.OnPaint( e );
			this.Paint( e );
		}

		protected override void OnResize( EventArgs e )
		{
			base.OnResize( e );
			ResizeNow( e );
		}

		private void ResizeNow( EventArgs ea )
		{
			this.Refresh();
		}

		private new void Paint( PaintEventArgs pea )
		{
			float _c1x1 = 0, _c1y1 = 0, _c1size = 0;
			float _c2x1 = 0, _c2y1 = 0, _c2size = 0;

			float _l1x1 = 0, _l1y1 = 0, _l1x2 = 0, _l1y2 = 0;
			float _l2x1 = 0, _l2y1 = 0, _l2x2 = 0, _l2y2 = 0;

			_c1size = this.SymbolSize * _trRatio;
			_c2size = this.SymbolSize;

			_l1x1 = _c2size / 2;
			_l1y1 = 0;
			_l1x2 = _l1x1;
			_l1y2 = _c1size * this._topHandRatio;

			_c1x1 = ( _c2size - _c1size ) / 2;
			_c1y1 = _l1y2;

			_c2x1 = 0;
			_c2y1 = _c1y1 + ( _c1size / 2 );

			_l2x1 = _c2size / 2;
			_l2y1 = _c2y1 + _c2size;
			_l2x2 = _l2x1;
			_l2y2 = _l2y1 + ( _c2size * this._bottomHandRatio );

			this.Height = ( int ) Math.Ceiling( _l2y2 );
			this.Width = ( int ) Math.Ceiling( _c2size + 1 );

			System.Drawing.Pen _pen = System.Drawing.Pens.Lime;
			System.Drawing.Brush _brush = System.Drawing.Brushes.Lime;
			System.Drawing.Drawing2D.PathGradientBrush _gBrush = null;
			System.Drawing.Drawing2D.GraphicsPath _gpath = new System.Drawing.Drawing2D.GraphicsPath();
			_pen = this.State == SymbolState.On ? System.Drawing.Pens.Red : System.Drawing.Pens.Lime;
			_brush = this.State == SymbolState.On ? System.Drawing.Brushes.Red : System.Drawing.Brushes.Lime;

			if ( this.Fill == false )
			{
				if ( this._type == TrasformerType.PotentialTrasformer )
				{
					_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, 120, 300 );
					_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, 120, 300 );
				}
				else if ( this._type == TrasformerType.AutoTrasformer )
				{
					_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, -30, 240 );
					_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, -30, 240 );
				}
				else if ( this._type == TrasformerType.TwoWindingTrasformer )
				{
					_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, 0, 360 );
					_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, 0, 360 );
					_gpath.AddArc( _c1x1 + ( int )( _c1size * .25 ), _c1y1 + ( int )( _c1size * .25 ), _c1size - ( int )( _c1size * .50 ), _c1size - ( int )( _c1size * .50 ), 0, 360 );
					_gpath.AddArc( _c2x1 + ( int )( _c2size * .25 ), _c2y1 + ( int )( _c2size * .25 ), _c2size - ( int )( _c2size * .50 ), _c2size - ( int )( _c2size * .50 ), 0, 360 );
				}
				else if ( this._type == TrasformerType.ThreeWindingTrasformer )
				{
					_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, 0, 360 );
					_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, 0, 360 );
					_gpath.AddArc( _c1x1 + ( int )( _c1size * .15 ), _c1y1 + ( int )( _c1size * .15 ), _c1size - ( int )( _c1size * .30 ), _c1size - ( int )( _c1size * .30 ), 0, 360 );
					_gpath.AddArc( _c2x1 + ( int )( _c2size * .15 ), _c2y1 + ( int )( _c2size * .15 ), _c2size - ( int )( _c2size * .30 ), _c2size - ( int )( _c2size * .30 ), 0, 360 );
					_gpath.AddArc( _c1x1 + ( int )( _c1size * .30 ), _c1y1 + ( int )( _c1size * .30 ), _c1size - ( int )( _c1size * .60 ), _c1size - ( int )( _c1size * .60 ), 0, 360 );
					_gpath.AddArc( _c2x1 + ( int )( _c2size * .30 ), _c2y1 + ( int )( _c2size * .30 ), _c2size - ( int )( _c2size * .60 ), _c2size - ( int )( _c2size * .60 ), 0, 360 );
				}
				else
				{
					_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, 0, 360 );
					_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, 0, 360 );
				}


			}
			else
			{
				_gpath.AddArc( _c1x1, _c1y1, _c1size, _c1size, 0, 360 );
				_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				_gBrush.CenterColor = this.Parent.BackColor;
				_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Lime };
				_gBrush.WrapMode = WrapMode.TileFlipY;
				pea.Graphics.FillPath( _gBrush, _gpath );
				_gpath.AddArc( _c2x1, _c2y1, _c2size, _c2size, 0, 360 );
				_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				_gBrush.CenterColor = this.Parent.BackColor;
				_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Lime };
				_gBrush.WrapMode = WrapMode.TileFlipY;
				pea.Graphics.FillPath( _gBrush, _gpath );
			}
			_gpath.CloseFigure();
			_gpath.AddLine( _l2x1, _l2y1, _l2x2, _l2y2 );
			_gpath.CloseFigure();
			_gpath.AddLine( _l1x1, _l1y1, _l1x2, _l1y2 );
			pea.Graphics.DrawPath( _pen, _gpath );
		}

	}
}
